package com.sky.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private HttpServletRequest request;


    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        //MD5密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return employee;
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    public ArrayList<Employee> searchEmployee(EmployeePageQueryDTO employeePageQueryDTO) {
        ArrayList<Employee> employees = new ArrayList<>();
        int start = (employeePageQueryDTO.getPage() - 1) * employeePageQueryDTO.getPageSize();
        employees = employeeMapper.searchEmployee(employeePageQueryDTO.getName(), start, employeePageQueryDTO.getPageSize());
        return employees;
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     * @return
     */
    public void addEmp(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        /*employee.setName(employeeDTO.getName());
        employee.setSex(employeeDTO.getSex());
        employee.setPhone(employeeDTO.getPhone());
        employee.setIdNumber(employeeDTO.getIdNumber());
        employee.setUsername(employeeDTO.getUsername());*/

        //对象拷贝
        BeanUtils.copyProperties(employeeDTO, employee);
        String password = PasswordConstant.DEFAULT_PASSWORD;//常量类
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
        employeeMapper.addEmp(employee);
    }

    /**
     * 员工分页查询记录数
     *
     * @param employeePageQueryDTO
     * @return Result<PageResult>
     */
    public Long getCount() {
        Long count = employeeMapper.getCount();
        return count;
    }

    /**
     * 员工帐号状态修改
     *
     * @return
     */
    public void changeStatus(int status, int id) {
        log.info(String.valueOf(status));
        employeeMapper.changeStatus(status, id);
    }

    /**
     * 修改密码
     *
     * @param passwordEditDTO
     * @return
     */
    public int changePassword(PasswordEditDTO passwordEditDTO) {
        passwordEditDTO.setEmpId(BaseContext.getCurrentId());
        String password = employeeMapper.searchPassword(passwordEditDTO.getEmpId());
        log.info("原密码{}", password);
        String webOldPassword = DigestUtils.md5DigestAsHex(passwordEditDTO.getOldPassword().getBytes());
        String webNewPassword = DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes());
        if (!webOldPassword.equals(password)) {
            return 0;
        }
        if (webNewPassword.equals(password)) {
            return 2;
        }
        passwordEditDTO.setNewPassword(webNewPassword);
        BaseContext.removeCurrentId();
        employeeMapper.changePassword(passwordEditDTO);
        return 1;
    }

    /**
     * 修改员工信息
     *
     * @param employeeDTO
     * @return
     */
    public void changeEmp(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employeeMapper.changeEmp(employee);
    }


    /**
     * 根据ID查询员工回显
     *
     * @param id
     * @return
     */
    public Employee searchId(int id) {
        Employee employee = employeeMapper.searchId(id);
        return employee;
    }
}